﻿Imports System.Windows.Forms

Public Class PeriodsDialog


    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'validate
        'check if this is a valid period

        If Len(PeriodBox.Text) <> 7 Then
            PeriodBox.Focus()

            MessageBox.Show("This is not a valid Period", "Invalid Period", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        End If


        If PeriodBox.Enabled Then
            'check if the period exists
            Dim PeriodId As Integer

            PeriodId = Mid(PeriodBox.Text, 1, 4) & Mid(PeriodBox.Text, 6, 2) & "1"

            Dim sql As String
            sql = "select * from periods where Period_ID = " & PeriodId

            If LoadDataView(sql).Count <> 0 Then
                PeriodBox.Focus()

                MessageBox.Show("This period already exists, please enter another period", "Period exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub StartDateBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartDateBox.ValueChanged
        If EndDateBox.Value.Date < StartDateBox.Value.Date Then
            EndDateBox.Value = StartDateBox.Value
        End If
    End Sub

    Private Sub EndDateBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndDateBox.ValueChanged
        If EndDateBox.Value.Date < StartDateBox.Value.Date Then
            StartDateBox.Value = EndDateBox.Value
        End If
    End Sub
End Class
